window.APP = window.APP || {
  Routers: {}, 
  Views: {},
};
(function() {
  "use strict";
  window.onerror = function(error, url, line) {
    if (DEBUG){
      console.error("--------------------------");
      console.error('Error name: '+ error + ' in file: '+url+' line: '+line);
      console.error("--------------------------");
    }
    return true; 
  }
  APP.Routers.MainRouter = Backbone.Router.extend({
    routes: {
      "": "",
    },
    current : function(route){
      if(route && Backbone.History.started) {
        var Router = this,
        // Get current fragment from Backbone.History
        fragment = Backbone.history.fragment,
        // Get current object of routes and convert to array-pairs
        routes = _.pairs(Router.routes);
        
        // Loop through array pairs and return
        // array on first truthful match.
        var matched = _.find(routes, function(handler) {
          var route = handler[0];
          
          // Convert the route to RegExp using the 
          // Backbone Router's internal convert
          // function (if it already isn't a RegExp)
          route = _.isRegExp(route) ? route :  Router._routeToRegExp(route);
          
          // Test the regexp against the current fragment
          return route.test(fragment);
        });
        
        // Returns callback name or false if 
        // no matches are found
        return matched ? matched[1] : false;
        // Just return current hash fragment in History
        return Backbone.history.fragment
      }
    },
    initialize: function (options) {
      Dropzone.autoDiscover = false;
      this.topBarView = null;
      this.language = options.language;
      APP.User = new User();
      $('.rightMenu span[data-lang="'+this.language+'"]').addClass('selected');
      Backbone.history.start({
        pushState: true, 
        root:'/'+ (this.language != DEFAULT_LANGUAGE ? this.language : '')
      });
      // Use absolute URLs to navigate to anything not in your Router.
 
      // Only need this for pushState enabled browsers
      if (Backbone.history && Backbone.history._hasPushState) {
 
        // Use delegation to avoid initial DOM selection and allow all matching elements to bubble
        $(document).delegate("a", "click", function(evt) {
          // Get the anchor href and protcol
          var href = $(this).attr("href");
          var protocol = this.protocol + "//";
 
          // Ensure the protocol is not part of URL, meaning its relative.
          // Stop the event bubbling to ensure the link will not cause a page refresh.
          if ($(evt.currentTarget).hasClass('allow-default')){
            
          } else if ($(evt.currentTarget).hasClass('prev-default')){
            evt.preventDefault();
          } else if (href !== undefined)
            if (href.slice(protocol.length) !== protocol) {
              evt.preventDefault();
 
              // Note by using Backbone.history.navigate, router events will not be
              // triggered. If this is a problem, change this to navigate on your
              // router.
              Backbone.history.navigate(href, true);
            }
        });
      }
    },
    before: function(route) {
    
    },
    after: function() {

    }
  });
}());

function isFunction(functionToCheck) {
  var getType = {};
  return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
}